﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class Department
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public string DisplayName
        {
            get
            {
                return string.Format("{0}{1}", Name, IsDeleted ? " (已删除)" : "");
            }
        }

        /// <summary>
        /// </summary>
        public IList<Employee> Employees
        {
            get
            {
                return InternalEmployees.Where(employee => employee.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Add & Remove Employee

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public Employee AddEmployee(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var employee = new Employee
                               {
                                   RowId = Guid.NewGuid(),
                                   CreatedDate = DateTime.Now,
                               };
            InternalEmployees.Add(employee);
            employee.IsDeleted = false;
            employee.ModifiedDate = DateTime.Now;
            employee.ModifiedByUser = modifiedUser;
            return employee;
        }

        /// <summary>
        /// </summary>
        /// <param name = "employee"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveEmployee(Employee employee, User modifiedUser)
        {
            if (employee == null)
            {
                throw new ArgumentNullException("employee");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalEmployees.Contains(employee))
            {
                employee.Department = null;
                employee.ModifiedDate = DateTime.Now;
                employee.ModifiedByUser = modifiedUser;
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (string.IsNullOrEmpty(Name))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "Department", "Name"));
            }
            Name = Name.Length > 64 ? Name.Substring(0, 64) : Name;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<Employee>) Employees).ForEach(employee =>
                                                     {
                                                         employee.Department = null;
                                                         employee.ModifiedDate = DateTime.Now;
                                                         employee.ModifiedByUser = modifiedUser;
                                                     });
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}